package me.zk.practice1.s;

import java.util.Arrays;

/**
 * @Author: zhang kai
 * @Date: 2023/05/05
 * @Description:
 */
public class S8 {
    public static void main(String[] args) {
        System.out.println(candy(new int[]{1,3,2,2,1}));
    }
    public static int candy(int[] ratings) {
        if (ratings.length < 2) {
            return ratings.length;
        }
        int[] num = new int[ratings.length];
        Arrays.fill(num,1);
        for (int i = 1; i < ratings.length; i++) {
            if (ratings[i] > ratings[i-1]) {
               num[i] = num[i-1] + 1;
            }
        }
        for (int i = ratings.length-1; i > 0; i--) {
            if (ratings[i] < ratings[i-1]) {
                num[i-1] = Math.max(num[i-1], num[i]+1);
            }
        }
        return Arrays.stream(num).sum();
        
    }
}
